﻿#include "linechart.h"

#include <QtCharts>     //折线图有关
#include <QChartView>
#include <QLineSeries>

#include <qmath.h>

LineChart::LineChart()
{

}

LineChart::~LineChart()
{

}

/*******************************************************************
 * 函数名：     tabWidgetAddChart
 * 函数作用：   将一个折线图添加到tabwidget中
 * 参数：      QList<double> datalist      测试数据列表
 *            QList<double> specslist     测试规格列表  格式：中心线，控制上限，控制下限，规格上限，规格下限
 *            int datalength              测试数据datalist的长度
 *            QString title               每张折线图的表名（测点名称），用于设置TabWidget每一页的标题
 * 返回值：     无
*******************************************************************/
void LineChart::tabWidgetAddChart(QTabWidget * tabWidget, QList<double> datalist, QList<double> specslist, QList<QString> datetime, int datalength, QString title)
{
    QChart * chart = new QChart();
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);

    auto lines = new QLineSeries();

    auto cllines = new QLineSeries();
    auto ucllines = new QLineSeries();
    auto lcllines = new QLineSeries();
    auto usllines = new QLineSeries();
    auto lsllines = new QLineSeries();

    lines->setName(QString::fromLocal8Bit("数据走势"));          //数据线
    cllines->setName(QString::fromLocal8Bit("中心线"));
    ucllines->setName(QString::fromLocal8Bit("控制上限"));
    lcllines->setName(QString::fromLocal8Bit("控制下限"));
    usllines->setName(QString::fromLocal8Bit("规格上限"));
    lsllines->setName(QString::fromLocal8Bit("规格下限"));

    QScatterSeries *series0 = new QScatterSeries();             //数据点。
    series0->setName(QString::fromLocal8Bit("数据点"));          //设置数据点名称
    series0->setColor(Qt::red);
    series0->setMarkerShape(QScatterSeries::MarkerShapeCircle); //设置数据点形状 圆形
    series0->setMarkerSize(10.0);               //设置数据点大小

    //QDateTimeAxis *axisX = new QDateTimeAxis;   //设置X轴 水平坐标 显示格式，显示日期时间
    //axisX->setTickCount(10);                    //显示10个坐标
    //axisX->setFormat("yyyy-MM-dd hh:mm:ss");  //以 2015 02 06 格式显示日期，不显示时间
    //axisX->setTitleText("Date");              //设置X轴名称
    //chart->addAxis(axisX, Qt::AlignBottom);     //设置显示在底面

    QValueAxis *axisY = new QValueAxis;         //设置Y轴 垂直坐标 显示格式，显示值
    axisY->setTickCount(5);                     //显示7个坐标
    axisY->setMax(specslist.value(3) + 0.02);
    axisY->setMin(specslist.value(4) - 0.02);
    axisY->setLabelFormat("%.3f");              //以精确度小数点3位的浮点数显示
    chart->addAxis(axisY, Qt::AlignLeft);       //设置显示在左边

    QDateTime momentInTime;

    QStringList date;
    QStringList time;

    //数据线赋值
    for(int m = 0;m < datalength; m++)   //为每个坐标点添加数据
    {
        //QString str0 = datetime[m].split(" ")[0];
        //QString str1 = datetime[m].split(" ")[1];
        //date = str0.split("-");
        //time = str1.split(":");
        //momentInTime.setDate(QDate(date[0].toInt(), date[1].toInt(), date[2].toInt()));
        //momentInTime.setTime(QTime(time[0].toInt(), time[1].toInt(), time[2].toInt()));

        lines->append(m, datalist.value(m));     //把时间转换成64位int数据
        series0->append(m, datalist.value(m));

        cllines->append(m, specslist.value(0));
        ucllines->append(m, specslist.value(1));
        lcllines->append(m, specslist.value(2));
        usllines->append(m, specslist.value(3));
        lsllines->append(m, specslist.value(4));
        //lsllines->append(momentInTime.toMSecsSinceEpoch(), specslist.value(4));
    }

    chart->addSeries(lines);    //将线性Lineseries加入图表
    //lines->attachAxis(axisX);
    lines->attachAxis(axisY);

    chart->addSeries(series0);  //将散射QScatterSeries加入图表
    //series0->attachAxis(axisX);
    series0->attachAxis(axisY);

    //设置线条风格与颜色
    QPen clpen;
    clpen.setStyle(Qt::DashLine);
    clpen.setColor(Qt::green);
    cllines->setPen(clpen);
    chart->addSeries(cllines);
    //cllines->attachAxis(axisX);
    cllines->attachAxis(axisY);

    QPen uclpen;
    uclpen.setStyle(Qt::DashLine);
    uclpen.setColor(Qt::darkYellow);
    ucllines->setPen(uclpen);
    chart->addSeries(ucllines);
    //ucllines->attachAxis(axisX);
    ucllines->attachAxis(axisY);

    QPen lclpen;
    lclpen.setStyle(Qt::DashLine);
    lclpen.setColor(Qt::darkYellow);
    lcllines->setPen(lclpen);
    chart->addSeries(lcllines);
    //lcllines->attachAxis(axisX);
    lcllines->attachAxis(axisY);

    QPen uslpen;
    uslpen.setStyle(Qt::DashLine);
    uslpen.setColor(Qt::red);
    usllines->setPen(uslpen);
    chart->addSeries(usllines);
    //usllines->attachAxis(axisX);
    usllines->attachAxis(axisY);

    QPen lslpen;
    lslpen.setStyle(Qt::DashLine);
    lslpen.setColor(Qt::red);
    lsllines->setPen(lslpen);
    chart->addSeries(lsllines);
    //lsllines->attachAxis(axisX);
    lsllines->attachAxis(axisY);

    auto cv = new QChartView(chart);
    cv->setRenderHint(QPainter::Antialiasing);      //设置抗锯齿
    tabWidget->addTab(cv, QString::fromLocal8Bit("%1").arg(title)); //将图表加入tabWidget当中，并设置标题
}

